<?php
namespace app\admin\controller;

use \think\Controller as Controller;
use \think\Cache as Cache;
use \think\Validate as Validate;
use \think\Config;
class Book extends Base{
    public function index(){
        $model = new \app\admin\model\Book;
        $book_data = $model->alias('bk')->join('book_type bt','bk.bt_id = bt.bt_id')->paginate();
        $this->assign('book_data',$book_data);
        
        return $this->fetch();
    }
    
    public function book(){
        
    }

    /**
     * 添加课本页面
     * 
     * @return 
     * @date Fri Jun 29 15:17:05 2018
     */
    public function add(){
        $model = new \app\admin\model\BookType;
        $typedata = $model->field('bt_id,bt_name')->select();
        $this->assign('typedata',$typedata);
        return $this->fetch();
    }

    /**
     * 插入书本数据
     * 
     * @return 
     * @date Fri Jun 29 16:10:45 2018
     */
    public function insert(){
        $post = $this->req->post();
        $rule = [
            'name' => 'require',
        ];
        $validate = new Validate($rule);

        // $result = $validate->check($post);
        if($validate->check($post)){
            $model = new \app\admin\model\Book;
            $data = [
                'bt_id' => $post['btid'],
                'bk_name' => $post['name'],
                'bk_sn' => $post['sn'],
                'bk_sort' => $post['sort']
            ];
            if($model->save($data)){
                echo $this->alert(1,'插入成功');
            } else {
                echo $this->alert(0,'插入失败');
            }
            
        } else{
            echo $this->alert(0,'没有输入名称1');
        }
        return;


    }

    public function edit(){
        $id = $this->req->get();
        $rule = [
            'id' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($id)){
            $type_model = new \app\admin\model\BookType;
            $book_model = new \app\admin\model\Book;
            // $book_data = $book_model->where(['bk_id'=>$id['id']])->find();
            $book_data = $book_model->get($id['id']);
            $type_data = $type_model->select();
            foreach($type_data as $t){
                if($t['bt_id'] == $book_data['bt_id']){
                    $t['selected'] = 'selected';
                }else{
                    $t['selected'] = '';
                }
            }
            $this->assign('book_data',$book_data);
            $this->assign('type_data',$type_data);
            exit($this->fetch());
        }else{
            exit($this->fetch('public/error',['msg'=>'id不正确']));
        }

    }

    /**
     * 更新书本信息
     * 
     * @return json
     * @date Sat Jun 30 19:21:54 2018
     */
    public function update(){
        $post = $this->req->post();
        $rule = [
            'bkid' => 'require',
            'name' => 'require',
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new \app\admin\model\Book;
            $data = [
                'bt_id' => $post['btid'],
                'bk_name' => $post['name'],
                'bk_sn' => $post['sn'],
                'bk_sort' => $post['sort']
            ];
            if($model->where(['bk_id'=>$post['bkid']])->update($data)){
                echo $this->alert(1,'更新成功');
            } else {
                echo $this->alert(0,'更新失败');
            }
            
        } else{
            echo $this->alert(0,'没有输入名称1');
        }
        return;

    }

    public function remove(){
        if($this->req->has('id')){
            $id = $this->req->get('id');
            $model = new \app\admin\model\Book;
            $result = $model->destroy($id);
            // var_dump($model->getLastSql());exit();
            // $result = $model->remove();
            // $result = 0;
            if($result){
                echo $this->wrong(['msg'=>'删除失败']);
            } else {
                echo $this->right(['msg'=>'删除成功']);
            }
        }else{
            return;
        }

    }

    public function chapter(){
        if($this->req->has('id')){
            $id = $this->req->get('id');
            $model = new \app\admin\model\BookChapter;
            $data = $model->where(['bk_id'=>$id])->select();
            if($data){
                $this->assign('data',$data);
                return $this->fetch();
            } else {
                return $this->err();
            }
        }else{
            return $this->fetch();
            // return;
        }
    }
    
}

